﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BCMS.FrontEnd.DataContract;
using BCMS.Settings;
using BCMS.Business.Implementation;

namespace BCMS.Forms.Functions
{
    public partial class frmCouponExtend : Form
    {
        CouponUI cp = null;

        public frmCouponExtend()
        {
            this.Font = SystemFonts.MessageBoxFont;
            InitializeComponent();
        }

        private List<string> getSerialsFromUI()
        {
            string[] series = txtNumCode.Text.Split('-');
            int clow = Convert.ToInt32(series[0]);
            int chigh = Convert.ToInt32(series[1]);
            List<string> list = new List<string>();
            for (int i = clow; i <= chigh; i++)
                list.Add(txtAlphaCode.Text + "-" + i.ToString());

            return list;
        }

        private void btnExtend_Click(object sender, EventArgs e)
        {
            try
            {
                DALclient client = new DALclient();
                string[] series = txtNumCode.Text.Split('-');

                if (series.Length > 1)
                {
                    client.batchCouponExtend(getSerialsFromUI(), txtNote.Text, dtExpire.Value.Date);
                    GlobalVars.ShowInfo("Gia hạn [THÀNH CÔNG] " + getSerialsFromUI().Count + " phiếu.");
                }
                else
                {
                    if (cp != null)
                    {
                        if (cp.Status.Equals(Params.CLAIMED))
                        {
                            GlobalVars.ShowWarning("Coupon: [" + cp.Serial +
                                            "] [ĐÃ ĐƯỢC THU HỒI] bởi [" + cp.ClaimBy + "]");
                            ResetUI();
                            return;
                        }

                        if (cp.Status.Equals(Params.DISPOSED))
                        {
                            GlobalVars.ShowWarning("Coupon: [" + cp.Serial +
                                            "] [ĐÃ ĐƯỢC HỦY] bởi [" + cp.ClaimBy + "]");
                            ResetUI();
                            return;
                        }

                        if (cp.ExpireDate > dtExpire.Value)
                        {
                            GlobalVars.ShowWarning("Ngày gia hạn [" + dtExpire.Value.ToString("dd/MM/yyyy") +
                                                "] phải xa hơn ngày hết hạn cũ [" +
                                                cp.ExpireDate.ToString("dd/MM/yyyy") + "]");
                            dtExpire.Focus();
                            return;
                        }

                        btnExtend.Enabled = false;
                        client.extendCouponBySerial(getSerialFromUI(), txtNote.Text, dtExpire.Value);
                        cp.Note = txtNote.Text;
                        GlobalVars.ShowInfo("Gia hạn [THÀNH CÔNG] coupon: [" + getSerialFromUI() + "]");

                        ResetUI();
                    }
                }
            }
            catch (Exception ex)
            {
                GlobalVars.ShowError("Không gia hạn được coupon." + "\nError: " + ex.Message);
                return;
            }
        }

        private void txtNumCode_Leave(object sender, EventArgs e)
        {
            try
            {
                string[] series = txtNumCode.Text.Split('-');

                if (series.Length.Equals(1))
                {
                    getCouponInfo(txtAlphaCode.Text + "-" + txtNumCode.Text);
                }
                else
                {
                    txtReleaseName.Text = string.Empty;
                    txtTarget.Text = string.Empty;
                    txtKind.Text = string.Empty;
                    txtDetail.Text = string.Empty;
                    txtStatus.Text = string.Empty;
                    txtStartDate.Text = string.Empty;
                    txtParValue.Text = string.Empty;

                    txtNote.Text = "Gia hạn theo lô: " + DateTime.Now.ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error getting coupon. Error: " + ex.Message);
            }
        }

        private string getSerialFromUI()
        {
            return txtAlphaCode.Text + "-" + txtNumCode.Text;
        }

        private void getCouponInfo(string serial)
        {
            try
            {
                DALclient client = new DALclient();
                cp = client.getCouponBySerial(serial);

                if (cp != null)
                {
                    txtParValue.Value = Convert.ToInt64(cp.ParValue);
                    txtStartDate.Text = cp.StartDate.ToString("dd/MM/yyyy");
                    dtExpire.Value = cp.ExpireDate;
                    txtReleaseName.Text = cp.ReleaseName.ToString();
                    txtStatus.Text = cp.Status;
                    txtTarget.Text = cp.ReleaseTarget;
                    txtKind.Text = cp.ReleaseKind;
                    txtDetail.Text = cp.ReleaseDetail;
                    txtRNote.Text = cp.ReleaseNote;
                    txtNote.Text = cp.Note;

                    if (cp.Status.Equals(Params.NORMAL) || cp.Status.Equals(Params.EXPIRED))
                        btnExtend.Enabled = true;

                    ReleaseUI rls = client.getReleaseByName(cp.ReleaseName);
                    if (!cp.ExpireDate.Equals(rls.ExpireDate.Date))
                        btnUndo.Enabled = true;

                }
                else
                {
                    GlobalVars.ShowWarning("Không tìm thấy coupon: [" +
                                    txtAlphaCode.Text + "-" + txtNumCode.Text + "]");
                    txtAlphaCode.SelectAll();
                }
            }
            catch (Exception ex)
            {
                GlobalVars.ShowError("Lỗi thu hồi coupons: " + txtAlphaCode.Text + "-" + txtNumCode.Text +
                                "\nError: " + ex.Message);
            }
        }

        private void ResetUI()
        {
            cp = null;
            btnUndo.Enabled = false;
            txtNumCode.Text = string.Empty;
            txtStartDate.Text = string.Empty;
            txtParValue.Text = string.Empty;
            txtReleaseName.Text = string.Empty;
            txtNote.Text = string.Empty;
            txtReleaseName.Text = string.Empty;
            txtTarget.Text = string.Empty;
            txtKind.Text = string.Empty;
            txtDetail.Text = string.Empty;
            txtStatus.Text = string.Empty;

            txtAlphaCode.SelectAll();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            const int WM_KEYDOWN = 0x100;
            if (msg.Msg == WM_KEYDOWN)
            {
                if (keyData == Keys.Escape)
                {
                    this.Close();
                    return true;
                }
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}
